import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_app/http/http_utils.dart';

import '../utils.dart';

class Demo04Widget extends StatelessWidget {
  final _accountController = TextEditingController();
  final _pwController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('表单'),
      ),
      body: Builder(builder: (BuildContext context) {
        return SafeArea(
            child: ListView(
          padding: EdgeInsets.symmetric(horizontal: 24.0),
          children: <Widget>[
            SizedBox(
              height: 80.0,
            ),
            _buildTitle(),
            SizedBox(height: 80),
            _buildAccount(),
            SizedBox(height: 12.0),
            _buildPassWord(),
            _buildFormButton(context)
          ],
        ));
      }),
    );
  }

  Widget _buildTitle() {
    return Image.asset(
      'images/logo_launch.png',
      width: 200,
      height: 50,
      fit: BoxFit.contain,
    );
  }

  Widget _buildAccount() {
    return TextField(
      controller: _accountController,
      decoration: InputDecoration(
          contentPadding: EdgeInsets.all(5),
          filled: true,
          labelText: '账号',
          labelStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
          prefixIcon: Icon(
            Icons.supervisor_account,
            color: Colors.blueAccent,
          ),
          border: InputBorder.none),
    );
  }

  Widget _buildPassWord() {
    return TextField(
      controller: _pwController,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(5),
        filled: true,
        labelStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
        labelText: '密码',
        prefixIcon: Icon(
          Icons.lock,
          color: Colors.blueAccent,
        ),
        suffixIcon: IconButton(
          icon: Icon(Icons.remove_red_eye),
          onPressed: () {},
        ),
        border: InputBorder.none,
      ),
      obscureText: true,
    );
  }

  Widget _buildFormButton(BuildContext context) {
    return ButtonBar(
      children: <Widget>[
        FlatButton(
          child: Text('清空'),
          onPressed: () {
            _accountController.clear();
            _pwController.clear();
          },
        ),
        RaisedButton(
            child: Text('登录'),
            onPressed: () {
              _login();
            })
      ],
    );
  }

  void _login() {
    //登录
    final username = _accountController.text;
    final pw = _pwController.text;

    FormData formData =
        FormData.from({"username": username, "pwd": Utils.md5(pw)});
    //测试请求
    HttpUtils()
        .get("http://t.weather.sojson.com/api/weather/city/101030100")
        .then((response) {
      print(response);
    });
  }
}
